﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Highlearner
{
	class CListViewAnimator
	{
		private Timer m_oTimer = new System.Windows.Forms.Timer();
		public int BlankImageIndex = -1;
		public int BlinkImageIndex = 1;

		public ListView ContainerListView = null;

		private SortedList<String, ListViewItem> m_oAnimatedItems = new SortedList<string,ListViewItem>();


		public void AddItem(ListViewItem a_oItem, String a_sKey)
		{
			m_oAnimatedItems.Add(a_sKey, a_oItem);

			StartEverone();
		}

		public void RemoveItem(ListViewItem a_oItem)
		{
			RemoveItem(m_oAnimatedItems.Keys[m_oAnimatedItems.IndexOfValue(a_oItem)]);
		}

		public void RemoveItem(String a_sKey)
		{
			m_oAnimatedItems.Remove(a_sKey);

			if (m_oAnimatedItems.Count == 0)
			{
				m_oTimer.Stop();
			}
		}

		public CListViewAnimator(ListView a_oContainerListView)
		{
			m_oTimer.Interval = 300;
			m_oTimer.Tick += new EventHandler(m_oTimer_Tick);

			ContainerListView = a_oContainerListView;
		}

		void m_oTimer_Tick(object sender, EventArgs e)
		{
			foreach (ListViewItem l_oItem in m_oAnimatedItems.Values)
			{
				if (l_oItem.ImageIndex != BlinkImageIndex)
				{
					l_oItem.ImageIndex = BlinkImageIndex;
				}
				else
				{
					l_oItem.ImageIndex = BlankImageIndex;
				}
			}
			ContainerListView.Refresh();
		}

		public void StartEverone()
		{
			m_oTimer.Start();
		}

		public void Stop()
		{
			m_oTimer.Stop();
		}


	}

}
